package com.whnk.web.controller.pm.project_manage;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import com.whnk.pm.domain.BaseInfo;
import com.whnk.pm.domain.ProjectInfo;
import com.whnk.pm.service.IProjectInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.whnk.common.annotation.Log;
import com.whnk.common.enums.BusinessType;

import com.whnk.framework.web.base.BaseController;
import com.whnk.framework.web.page.TableDataInfo;
import com.whnk.common.base.AjaxResult;

/**
 * 项目 信息操作处理
 * 
 * @author whwhnk
 * @date 2020-01-09
 */
@Controller
@RequestMapping("/pm/projectInfo")
public class ProjectInfoController extends BaseController
{
    private String prefix = "pm/projectInfo";
	
	@Autowired
	private IProjectInfoService projectInfoService;
	
	@RequiresPermissions("pm:projectInfo:view")
	@GetMapping()
	public String projectInfo()
	{
	    return prefix + "/projectInfo";
	}
	
	/**
	 * 查询项目列表
	 */
	@RequiresPermissions("pm:projectInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProjectInfo projectInfo)
	{
		startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoList(projectInfo);
		return getDataTable(list);
	}
	
	/**
	 * 新增项目
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存项目
	 */
	@RequiresPermissions("pm:projectInfo:add")
	@Log(title = "项目", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ProjectInfo projectInfo)
	{		
		return toAjax(projectInfoService.insertProjectInfo(projectInfo));
	}

	/**
	 * 修改项目
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		ProjectInfo projectInfo = projectInfoService.selectProjectInfoById(id);
		mmap.put("projectInfo", projectInfo);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存项目
	 */
	@RequiresPermissions("pm:projectInfo:edit")
	@Log(title = "项目", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ProjectInfo projectInfo)
	{		
		return toAjax(projectInfoService.updateProjectInfo(projectInfo));
	}
	
	/**
	 * 删除项目
	 */
	@RequiresPermissions("pm:projectInfo:remove")
	@Log(title = "项目", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(projectInfoService.deleteProjectInfoByIds(ids));
	}

	@GetMapping("/budget/{id}")
	public String projectBudget(@PathVariable("id") String id, ModelMap mmap)
	{
		ProjectInfo projectInfo = projectInfoService.selectProjectInfoById(Integer.parseInt(id));
		mmap.put("projectInfo", projectInfo);
		return "pm/projectBudget/projectBudget";
	}

	@GetMapping("/procurementResource/{id}")
	public String projectProcurementResource(@PathVariable("id") String id, ModelMap mmap)
	{
		ProjectInfo projectInfo = projectInfoService.selectProjectInfoById(Integer.parseInt(id));
		mmap.put("projectInfo", projectInfo);
		return "pm/procurementResource/procurementResource";
	}

	@GetMapping("/companyProcurement/{id}")
	public String projectCompanyProcurement(@PathVariable("id") String id, ModelMap mmap)
	{
		ProjectInfo projectInfo = projectInfoService.selectProjectInfoById(Integer.parseInt(id));
		mmap.put("projectInfo", projectInfo);
		return "pm/companyProcurement/companyProcurement";
	}

	@PostMapping("/exportExcel")
	@ResponseBody
	public AjaxResult exportExcel() throws UnsupportedEncodingException {
		String filename = projectInfoService.exportExcel();
		String result = URLEncoder.encode(filename, "UTF-8");
		return AjaxResult.success(result);
	}
}
